{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "048bca0a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "906461c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv(\"./gaze.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb2774a4",
   "metadata": {},
   "source": [
    "# Exercise 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3d5f1e70",
   "metadata": {},
   "outputs": [],
   "source": [
    "# (1) confidence为可信度,值越高代表本条数据越可信,只保留0.9(含0.9)及以上confidence的数据,其他行数据丢弃,输出剩余数据条数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4c8b1c84",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "87442\n"
     ]
    }
   ],
   "source": [
    "result1 = data[data['confidence']>= 0.9]\n",
    "print(len(result1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35b1d770",
   "metadata": {},
   "source": [
    "# Exercise 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a76ef22c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# (2) norm_pos_x和norm_pos _y符合正态分布,利用3 sigma原则去除无效值,即去掉>mean+3sigma和<mean-3sigma的值：输出剩余数据条数."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b435a5c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean_x: 0.4006508623486318 std_x: 0.0480622048960461\n",
      "mean_y: 0.4143485845880635 std_y: 0.023909596833708923\n",
      "87062\n"
     ]
    }
   ],
   "source": [
    "eff_x = []\n",
    "for x in result1['norm_pos_x']:\n",
    "    eff_x.append(float(x))\n",
    "std_x = np.std(eff_x)\n",
    "mean_x = np.mean(eff_x)\n",
    "print('mean_x:',mean_x,'std_x:', std_x)\n",
    "max_x = mean_x + std_x *3\n",
    "min_x = mean_x - std_x *3\n",
    "eff_y=[]\n",
    "for y in result1['norm_pos_y']:\n",
    "    eff_y.append(float(y))\n",
    "mean_y = np.mean(eff_y)\n",
    "std_y = np.std(eff_y)\n",
    "print('mean_y:',mean_y, 'std_y:',std_y)\n",
    "max_y = mean_y + std_y * 3\n",
    "min_y = mean_y - std_y * 3\n",
    "result2= result1[(result1['norm_pos_x'] <= max_x ) & (result1['norm_pos_y'] >= min_x) & (result1['norm_pos_y'] <= max_y) & (result1['norm_pos_y'] >= min_y)]\n",
    "print(len(result2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9cce8864",
   "metadata": {},
   "source": [
    "# Exercise 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "daf2a77d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# (3）gaze timestamp列是时间戳(单位为s),为了方便分析人员查看,将其改为\"1970-01-02T17:44:30.225707+0000\"的形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "baf5572e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1970-01-02T 00:29:10.776780+0000\n",
      "1970-01-02T 00:29:10.776787+0000\n",
      "1970-01-02T 00:29:10.779709+0000\n",
      "1970-01-02T 00:29:10.779759+0000\n",
      "1970-01-02T 00:29:10.787485+0000\n",
      "1970-01-02T 00:29:10.787580+0000\n",
      "1970-01-02T 00:29:10.789480+0000\n",
      "1970-01-02T 00:29:10.793583+0000\n",
      "1970-01-02T 00:29:10.795683+0000\n",
      "1970-01-02T 00:29:10.798406+0000\n",
      "1970-01-02T 00:29:10.801140+0000\n",
      "1970-01-02T 00:29:10.802973+0000\n",
      "1970-01-02T 00:29:10.804631+0000\n",
      "1970-01-02T 00:29:10.807269+0000\n",
      "1970-01-02T 00:29:10.809749+0000\n",
      "1970-01-02T 00:29:10.812873+0000\n",
      "1970-01-02T 00:29:10.814136+0000\n",
      "1970-01-02T 00:29:10.815998+0000\n",
      "1970-01-02T 00:29:10.818051+0000\n",
      "1970-01-02T 00:29:10.821819+0000\n",
      "1970-01-02T 00:29:10.823541+0000\n",
      "1970-01-02T 00:29:10.825638+0000\n",
      "1970-01-02T 00:29:10.827729+0000\n",
      "1970-01-02T 00:29:10.829663+0000\n",
      "1970-01-02T 00:29:10.831638+0000\n",
      "1970-01-02T 00:29:10.833690+0000\n",
      "1970-01-02T 00:29:10.835701+0000\n",
      "1970-01-02T 00:29:10.839575+0000\n",
      "1970-01-02T 00:29:10.842234+0000\n",
      "1970-01-02T 00:29:10.843449+0000\n"
     ]
    }
   ],
   "source": [
    "gaze_time = data['gaze_timestamp']\n",
    "sample_trans = []\n",
    "for t in gaze_time:\n",
    "    trans = datetime.datetime.fromtimestamp(t,datetime.timezone.utc).strftime(\"%Y-%m-%dT %H:%M:%S.%f%z\")\n",
    "    sample_trans.append(trans)\n",
    "for i in range(30):\n",
    "    print(sample_trans[i])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89401be3",
   "metadata": {},
   "source": [
    "# Exercise 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b7b732d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# (4）高帧率采集设备在采集每帧时有一定的偏差,请问该数据的采样率是多少？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ac8a3e0b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "427.62952145975567\n"
     ]
    }
   ],
   "source": [
    "total = len(open(\"./gaze.csv\").readlines())\n",
    "sample_T = (data['gaze_timestamp'].iloc[-1] - data['gaze_timestamp'].iloc[0])/ total\n",
    "sample_fre = 1 / sample_T\n",
    "print(sample_fre)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd7c3086",
   "metadata": {},
   "source": [
    "# Exercise 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "007fb429",
   "metadata": {},
   "outputs": [],
   "source": [
    "# (5）高帧率采集设备在采集每帧的时需要打上时间戳,但是时间戳有误差,每一帧时间戳不是严格按照采样率,为了后期时序数据处理方便，将gaze_timestamp列按照100Hz的帧率重新采样,输出处理后的数据集的\n",
    "# 前20行数据，时间戳格式不限。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "61d0eb89",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['1970-01-02T 00:29:10.776780+0000', '1970-01-02T 00:29:10.787485+0000', '1970-01-02T 00:29:10.798406+0000', '1970-01-02T 00:29:10.807269+0000', '1970-01-02T 00:29:10.818051+0000', '1970-01-02T 00:29:10.831638+0000', '1970-01-02T 00:29:10.843449+0000', '1970-01-02T 00:29:10.858052+0000', '1970-01-02T 00:29:10.869620+0000', '1970-01-02T 00:29:10.879584+0000', '1970-01-02T 00:29:10.891700+0000', '1970-01-02T 00:29:10.905495+0000', '1970-01-02T 00:29:10.914199+0000', '1970-01-02T 00:29:10.927525+0000', '1970-01-02T 00:29:10.938559+0000', '1970-01-02T 00:29:10.947523+0000', '1970-01-02T 00:29:10.961489+0000', '1970-01-02T 00:29:10.973578+0000', '1970-01-02T 00:29:10.982021+0000', '1970-01-02T 00:29:10.995475+0000']\n"
     ]
    }
   ],
   "source": [
    "reList = []\n",
    "reList.append(data['gaze_timestamp'].loc[0])\n",
    "for i in range(0,len(data)):\n",
    "    tmp = data['gaze_timestamp'].iloc[i]+0.01\n",
    "    if tmp - reList[-1]>= 0.01:\n",
    "        reList.append(tmp)\n",
    "k = 0\n",
    "result5 = []\n",
    "for j in reList:\n",
    "    while k < len(data) and abs(data['gaze_timestamp'].iloc[k] - j) > abs(data['gaze_timestamp'].iloc[k+1] - j):\n",
    "        k += 1\n",
    "    tmp = datetime.datetime.fromtimestamp(data['gaze_timestamp'].iloc[k],datetime.timezone.utc).strftime(\"%Y-%m-%dT %H:%M:%S.%f%z\")\n",
    "    result5.append(tmp)\n",
    "print(result5[:20])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0204042c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
